package com.example.redislock.config;

import com.example.redislock.core.RedisReentrantLock;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * 分布式锁工厂，用于创建不同key的锁实例
 */
public class RedisLockFactory {

    private final RedisTemplate<String, Object> redisTemplate;
    private final RedisLockProperties properties;

    public RedisLockFactory(RedisTemplate<String, Object> redisTemplate, RedisLockProperties properties) {
        this.redisTemplate = redisTemplate;
        this.properties = properties;
    }

    /**
     * 根据锁键创建锁实例
     */
    public RedisReentrantLock getLock(String lockKey) {
        return new RedisReentrantLock(
                redisTemplate,
                lockKey,
                properties.getDefaultLockTimeout(),
                properties.getWatchDogInterval()
        );
    }

    /**
     * 根据锁键和超时时间创建锁实例
     */
    public RedisReentrantLock getLock(String lockKey, long lockTimeout) {
        return new RedisReentrantLock(
                redisTemplate,
                lockKey,
                lockTimeout,
                properties.getWatchDogInterval()
        );
    }
}
